
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
  <meta charset="utf-8">
  <title>许龙武的技术博客</title>
  <meta name="author" content="许龙武">

  
  <meta name="description" content="iOS开发中一直没找到方便的字符串管理工具，干脆自己写了这个插件。主要参考了一些之前一些很优秀的插件像XTodo和RTImageAssets等。第一次写插件，借机学习了一下Mac开发。这个插件支持Object-C和swift两种语言开发的项目，不过由于我还一直使用Object-C开发， &hellip;">
  

  <!-- http://t.co/dKP3o1e -->
  <meta name="HandheldFriendly" content="True">
  <meta name="MobileOptimized" content="320">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  
  <link rel="canonical" href="http://loongwoo.github.io/">
  <link href="/favicon.png" rel="icon">
  <link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
  <link href="/atom.xml" rel="alternate" title="许龙武的技术博客" type="application/atom+xml">
  <script src="/javascripts/modernizr-2.0.js"></script>
  <script src="http://cdn.staticfile.org/jquery/1.9.1/jquery.min.js"></script>
  <script>!window.jQuery && document.write(unescape('%3Cscript src="/javascripts/libs/jquery.min.js"%3E%3C/script%3E'))</script>
  <script src="/javascripts/octopress.js" type="text/javascript"></script>
  

</head>

<body   >
  <header role="banner"><hgroup>
  <h1><a href="/">许龙武的技术博客</a></h1>
  
    <h2>记录所学，快乐分享</h2>
  
</hgroup>

</header>
  <nav role="navigation"><ul class="subscription" data-subscription="rss">
  <li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
  
</ul>
  
<form action="https://www.google.com/search" method="get">
  <fieldset role="search">
    <input type="hidden" name="sitesearch" value="loongwoo.github.io">
    <input class="search" type="text" name="q" results="0" placeholder="Search"/>
  </fieldset>
</form>
  
<ul class="main-navigation">
	<li><a href="/">首页</a></li>
	<li><a href="/blog/archives">所有文章</a></li>
	<li><a href="http://weibo.com/loongwoo">我的微博</a></li>
</ul>

</nav>
  <div id="main">
    <div id="content">
      <div class="blog-index">
  
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2016/01/28/stringmanage-a-xcode-plugin-for-managing-your-strings/">StringManage -- a Xcode Plugin for Managing Your Strings</a></h1>
    
    
      <p class="meta">
        




<time class='entry-date' datetime='2016-01-28T11:07:51+08:00'><span class='date'><span class='date-month'>Jan</span> <span class='date-day'>28</span><span class='date-suffix'>th</span>, <span class='date-year'>2016</span></span> <span class='time'>11:07 am</span></time>
        
      </p>
    
  </header>


  <div class="entry-content"><p>iOS开发中一直没找到方便的字符串管理工具，干脆自己写了这个插件。主要参考了一些之前一些很优秀的插件像<a href="https://github.com/trawor/XToDo">XTodo</a>和<a href="https://github.com/rickytan/RTImageAssets">RTImageAssets</a>等。第一次写插件，借机学习了一下Mac开发。这个插件支持Object-C和swift两种语言开发的项目，不过由于我还一直使用Object-C开发，所以swift的支持可能问题会更多一点，以后再进一步完善。</p>

<h3>主界面介绍</h3>

<p>主界面</p>

<p><img src="/images/StringManage1.png" alt="主界面" /></p>

<p>使用<kbd>ctrl</kbd>+<kbd>shift</kbd>+<kbd>s</kbd>快捷组合键可以打开或关闭管理界面。</p>

<ul>
<li>show all the strings in Localizable.strings</li>
<li>add/remove/search strings</li>
<li>check every string is used or not and jump to the referenced line</li>
</ul>


<p>设置界面</p>

<p><img src="/images/StringManage2.png" alt="设置界面" /></p>

<p>支持从<a href="http://alcatraz.io">Alcatraz</a>下载本插件，也可以直接下载源码，运行后重启Xcode即可。</p>

<p><img src="/images/StringManage3.png" alt="Alcatraz截图" /></p>

<h3>项目源码</h3>

<p><a href="https://github.com/Loongwoo/StringManage">StringManage代码位置</a></p>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/09/12/iosshi-yong-bundle/">iOS使用Bundle</a></h1>
    
    
      <p class="meta">
        




<time class='entry-date' datetime='2015-09-12T18:47:28+08:00'><span class='date'><span class='date-month'>Sep</span> <span class='date-day'>12</span><span class='date-suffix'>th</span>, <span class='date-year'>2015</span></span> <span class='time'>6:47 pm</span></time>
        
      </p>
    
  </header>


  <div class="entry-content"><p>在生成SDK的时候我们常常把资源文件打包成一个Bundle。</p>

<h2>关于生成Bundle</h2>

<blockquote><ul>
<li>方法一：可以参考<a href="http://blog.sina.com.cn/s/blog_7b9d64af0101jmj2.html">iOS-生成Bundle包-引入bundle-使用bundle</a>。</li>
<li>方法二：不用这么麻烦的就新建一个文件夹，把资源放进去后给文件夹加一个后缀‘.bundle’即可。</li>
</ul>
</blockquote>

<h2>使用Bundle中的图片</h2>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="bp">UIImage</span> <span class="o">*</span><span class="n">image</span> <span class="o">=</span> <span class="p">[</span><span class="bp">UIImage</span> <span class="nl">imageNamed</span><span class="p">:</span><span class="s">@&quot;MyBundle.bundle/img_collect_success&quot;</span><span class="p">];</span>
</span></code></pre></td></tr></table></div></figure>


<h2>使用Bundle中的Localizable.Strings</h2>

<p>定义以下宏：</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="cp">#define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @&quot;MyBundle.bundle&quot;]</span>
</span><span class='line'><span class="cp">#define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]</span>
</span><span class='line'>
</span><span class='line'><span class="c1">//获取Bundle中的字符串</span>
</span><span class='line'><span class="cp">#define MYLocalizedString(key, comment) [MYBUNDLE localizedStringForKey:(key) value:@&quot;&quot; table:nil]</span>
</span></code></pre></td></tr></table></div></figure>


<p>如果不是命名为Localizable.Strings，比如xulongwu.Strings，那么最后一个宏就得改为：</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="c1">//获取Bundle中的字符串</span>
</span><span class='line'><span class="cp">#define MYLocalizedString(key, comment) [MYBUNDLE localizedStringForKey:(key) value:@&quot;&quot; table:@&quot;xulongwu&quot;]</span>
</span></code></pre></td></tr></table></div></figure>


<p>使用的时候如下：</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="bp">NSString</span> <span class="o">*</span><span class="n">str</span> <span class="o">=</span> <span class="n">MYLocalizedString</span><span class="p">(</span><span class="s">@&quot;Settings&quot;</span><span class="p">,</span> <span class="nb">nil</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>



</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/09/12/iosduo-ge-localizable-dot-strings/">IOS多个Localizable.strings</a></h1>
    
    
      <p class="meta">
        




<time class='entry-date' datetime='2015-09-12T18:21:16+08:00'><span class='date'><span class='date-month'>Sep</span> <span class='date-day'>12</span><span class='date-suffix'>th</span>, <span class='date-year'>2015</span></span> <span class='time'>6:21 pm</span></time>
        
      </p>
    
  </header>


  <div class="entry-content"><p>一般情况项目中只有一个Localizable.strings文件来管理多语言的本地化。这时候只要用<code>NSLocalizedString(key, comment)</code>就可以了。</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="bp">NSString</span> <span class="o">*</span><span class="n">str</span> <span class="o">=</span> <span class="n">NSLocalizedString</span><span class="p">(</span><span class="s">@&quot;Settings&quot;</span><span class="p">,</span> <span class="nb">nil</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>但如果需要多个Localizable.strings，比如还有一个KWLocalizable.strings的语言文件，这时候就可以用<code>NSLocalizedStringFromTable(key, tbl, comment)</code>来获取字符串了。可以定义一个宏：</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="cp">#define XLocalizedString(key, comment) NSLocalizedStringFromTable((key), @&quot;KWLocalizable&quot;, (comment))</span>
</span></code></pre></td></tr></table></div></figure>


<p>tbl就是新的文件的名字，使用的时候如下：</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="bp">NSString</span> <span class="o">*</span><span class="n">str</span> <span class="o">=</span> <span class="n">XLocalizedString</span><span class="p">(</span><span class="s">@&quot;Settings&quot;</span><span class="p">,</span> <span class="nb">nil</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>



</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/09/11/iosxian-zhi-uitextfieldchang-du/">IOS限制UITextField长度</a></h1>
    
    
      <p class="meta">
        




<time class='entry-date' datetime='2015-09-11T16:14:35+08:00'><span class='date'><span class='date-month'>Sep</span> <span class='date-day'>11</span><span class='date-suffix'>th</span>, <span class='date-year'>2015</span></span> <span class='time'>4:14 pm</span></time>
        
      </p>
    
  </header>


  <div class="entry-content"><ul>
<li>首先想到的是实现<code>UITextFieldDelegate</code>的以下方法</li>
</ul>


<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="p">-</span> <span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nf">textField:</span><span class="p">(</span><span class="bp">UITextField</span> <span class="o">*</span><span class="p">)</span><span class="nv">textField</span> <span class="nf">shouldChangeCharactersInRange:</span><span class="p">(</span><span class="n">NSRange</span><span class="p">)</span><span class="nv">range</span> <span class="nf">replacementString:</span><span class="p">(</span><span class="bp">NSString</span> <span class="o">*</span><span class="p">)</span><span class="nv">string</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="bp">NSUInteger</span> <span class="n">newLength</span> <span class="o">=</span> <span class="p">[</span><span class="nb">self</span><span class="p">.</span><span class="n">textField</span><span class="p">.</span><span class="n">text</span> <span class="n">length</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="n">string</span> <span class="n">length</span><span class="p">]</span> <span class="o">-</span> <span class="n">range</span><span class="p">.</span><span class="n">length</span><span class="p">;</span>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="n">maxLength</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="n">newLength</span> <span class="o">&gt;</span> <span class="n">maxLength</span><span class="p">)</span>
</span><span class='line'>        <span class="k">return</span> <span class="nb">NO</span><span class="p">;</span>
</span><span class='line'>    <span class="k">return</span> <span class="nb">YES</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<ul>
<li>这时候会发现输入中文时如果开了联想，以上的方法不会响应。那么就可以输入超过所设定的最大值。这时候可以加上监听<code>UIControlEventEditingChanged</code>事件来强制性截断输入值。</li>
</ul>


<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="p">[</span><span class="n">textField</span> <span class="nl">addTarget</span><span class="p">:</span><span class="nb">self</span> <span class="nl">action</span><span class="p">:</span><span class="k">@selector</span><span class="p">(</span><span class="nl">textFieldDidChange</span><span class="p">:)</span> <span class="nl">forControlEvents</span><span class="p">:</span><span class="n">UIControlEventEditingChanged</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'><span class="p">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">textFieldDidChange:</span><span class="p">(</span><span class="bp">UITextField</span> <span class="o">*</span><span class="p">)</span><span class="nv">textfield</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="n">textfield</span> <span class="o">==</span> <span class="nb">self</span><span class="p">.</span><span class="n">textField</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="n">textField</span><span class="p">.</span><span class="n">text</span><span class="p">.</span><span class="n">length</span> <span class="o">&gt;</span> <span class="n">maxLength</span><span class="p">)</span>
</span><span class='line'>        <span class="p">{</span>
</span><span class='line'>            <span class="n">textField</span><span class="p">.</span><span class="n">text</span> <span class="o">=</span> <span class="p">[</span><span class="n">textField</span><span class="p">.</span><span class="n">text</span> <span class="nl">substringToIndex</span><span class="p">:</span><span class="n">maxLength</span><span class="p">];</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>



</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/09/11/ioszi-dong-bu-ju-constraintswithvisualformat/">IOS自动布局constraintsWithVisualFormat</a></h1>
    
    
      <p class="meta">
        




<time class='entry-date' datetime='2015-09-11T11:37:47+08:00'><span class='date'><span class='date-month'>Sep</span> <span class='date-day'>11</span><span class='date-suffix'>th</span>, <span class='date-year'>2015</span></span> <span class='time'>11:37 am</span></time>
        
      </p>
    
  </header>


  <div class="entry-content"><p>在手敲代码的时候进行Autolayout就需要给控件添加constraint（约束）。下面演示UIView的addConstraints方法的使用：</p>

<h2>添加控件</h2>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="bp">UIButton</span> <span class="o">*</span><span class="n">quickbtn</span><span class="o">=</span><span class="p">[</span><span class="bp">UIButton</span> <span class="nl">buttonWithType</span><span class="p">:</span><span class="n">UIButtonTypeCustom</span><span class="p">];</span>
</span><span class='line'><span class="p">[</span><span class="n">quickbtn</span> <span class="nl">setBackgroundColor</span><span class="p">:[</span><span class="bp">UIColor</span> <span class="nl">colorFromHexString</span><span class="p">:</span><span class="s">@&quot;#00bfff&quot;</span><span class="p">]</span> <span class="nl">forState</span><span class="p">:</span><span class="n">UIControlStateNormal</span><span class="p">];</span>
</span><span class='line'><span class="p">[</span><span class="n">quickbtn</span> <span class="nl">setTitle</span><span class="p">:</span><span class="n">NSLocalizedString</span><span class="p">(</span><span class="s">@&quot;快点我&quot;</span><span class="p">,</span> <span class="nb">nil</span><span class="p">)</span> <span class="nl">forState</span><span class="p">:</span><span class="n">UIControlStateNormal</span><span class="p">];</span>
</span><span class='line'><span class="n">quickbtn</span><span class="p">.</span><span class="n">titleLabel</span><span class="p">.</span><span class="n">font</span><span class="o">=</span><span class="p">[</span><span class="bp">UIFont</span> <span class="nl">systemFontOfSize</span><span class="p">:</span><span class="mi">16</span><span class="p">];</span>
</span><span class='line'><span class="p">[</span><span class="n">quickbtn</span> <span class="nl">addTarget</span><span class="p">:</span><span class="nb">self</span> <span class="nl">action</span><span class="p">:</span><span class="k">@selector</span><span class="p">(</span><span class="nl">_quickAction</span><span class="p">:)</span> <span class="nl">forControlEvents</span><span class="p">:</span><span class="n">UIControlEventTouchUpInside</span><span class="p">];</span>
</span><span class='line'><span class="p">[</span><span class="nb">self</span><span class="p">.</span><span class="n">view</span> <span class="nl">addSubview</span><span class="p">:</span><span class="n">quickbtn</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'><span class="bp">UIButton</span> <span class="o">*</span><span class="n">nextbtn</span><span class="o">=</span><span class="p">[</span><span class="bp">UIButton</span> <span class="nl">buttonWithType</span><span class="p">:</span><span class="n">UIButtonTypeCustom</span><span class="p">];</span>
</span><span class='line'><span class="p">[</span><span class="n">nextbtn</span> <span class="nl">setBackgroundColor</span><span class="p">:</span><span class="n">KWColor</span> <span class="nl">forState</span><span class="p">:</span><span class="n">UIControlStateNormal</span><span class="p">];</span>
</span><span class='line'><span class="p">[</span><span class="n">nextbtn</span> <span class="nl">setTitle</span><span class="p">:</span><span class="n">NSLocalizedString</span><span class="p">(</span><span class="s">@&quot;别点我&quot;</span><span class="p">,</span> <span class="nb">nil</span><span class="p">)</span> <span class="nl">forState</span><span class="p">:</span><span class="n">UIControlStateNormal</span><span class="p">];</span>
</span><span class='line'><span class="n">nextbtn</span><span class="p">.</span><span class="n">titleLabel</span><span class="p">.</span><span class="n">font</span><span class="o">=</span><span class="p">[</span><span class="bp">UIFont</span> <span class="nl">systemFontOfSize</span><span class="p">:</span><span class="mi">16</span><span class="p">];</span>
</span><span class='line'><span class="p">[</span><span class="n">nextbtn</span> <span class="nl">addTarget</span><span class="p">:</span><span class="nb">self</span> <span class="nl">action</span><span class="p">:</span><span class="k">@selector</span><span class="p">(</span><span class="nl">_nextAction</span><span class="p">:)</span> <span class="nl">forControlEvents</span><span class="p">:</span><span class="n">UIControlEventTouchUpInside</span><span class="p">];</span>
</span><span class='line'><span class="p">[</span><span class="nb">self</span><span class="p">.</span><span class="n">view</span> <span class="nl">addSubview</span><span class="p">:</span><span class="n">nextbtn</span><span class="p">];</span>
</span></code></pre></td></tr></table></div></figure>


<p>添加了两个按钮，注意到还没有指定frame，我的要求是把他们放到屏幕底部，左右分开同高同宽，中间放一个像素的空隙。这里一般的方法是算出他们的frame后直接指定，还可以用autoresizingMask设置一下。但是考虑到算起来麻烦，可以用constraint进行相对布局。</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="n">quickbtn</span><span class="p">.</span><span class="n">autoresizingMask</span> <span class="o">=</span> <span class="n">UIViewAutoresizingFlexibleWidth</span> <span class="o">|</span> <span class="n">UIViewAutoresizingFlexibleTopMargin</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<h2>constraintsWithVisualFormat</h2>

<ol>
<li>首先关闭AutoresizingMask</li>
</ol>


<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="n">quickbtn</span><span class="p">.</span><span class="n">translatesAutoresizingMaskIntoConstraints</span><span class="o">=</span><span class="nb">NO</span><span class="p">;</span>
</span><span class='line'><span class="n">nextbtn</span><span class="p">.</span><span class="n">translatesAutoresizingMaskIntoConstraints</span><span class="o">=</span><span class="nb">NO</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<ol>
<li>添加约束</li>
</ol>


<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="c1">//format: 约束语句</span>
</span><span class='line'><span class="c1">//options: 置为0</span>
</span><span class='line'><span class="c1">//metrics: 可以把参数写在这里，format中就可以直接制定key</span>
</span><span class='line'><span class="c1">//views: 通过NSDictionaryOfVariableBindings()把要添加约束的控件加进来就可以了</span>
</span><span class='line'><span class="p">+</span> <span class="p">(</span><span class="bp">NSArray</span> <span class="o">*</span><span class="p">)</span><span class="nf">constraintsWithVisualFormat:</span><span class="p">(</span><span class="bp">NSString</span> <span class="o">*</span><span class="p">)</span><span class="nv">format</span> <span class="nf">options:</span><span class="p">(</span><span class="n">NSLayoutFormatOptions</span><span class="p">)</span><span class="nv">opts</span> <span class="nf">metrics:</span><span class="p">(</span><span class="bp">NSDictionary</span> <span class="o">*</span><span class="p">)</span><span class="nv">metrics</span> <span class="nf">views:</span><span class="p">(</span><span class="bp">NSDictionary</span> <span class="o">*</span><span class="p">)</span><span class="nv">views</span>
</span></code></pre></td></tr></table></div></figure>


<p>下面是代码，<code>constraintsWithVisualFormat</code>的第一个参数format的写法注释了。</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="c1">//等于NSDictionary *views=@{@&quot;quickbtn&quot;:quickbtn, @&quot;nextbtn&quot;:nextbtn}；</span>
</span><span class='line'><span class="bp">NSDictionary</span> <span class="o">*</span><span class="n">views</span><span class="o">=</span><span class="n">NSDictionaryOfVariableBindings</span><span class="p">(</span><span class="n">quickbtn</span><span class="p">,</span> <span class="n">nextbtn</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="c1">//V:表示竖向，[quickbtn(==44)]表示quickbtn高度为44，-10-|表示quickbtn距离底部10像素，</span>
</span><span class='line'><span class="p">[</span><span class="nb">self</span><span class="p">.</span><span class="n">view</span> <span class="nl">addConstraints</span><span class="p">:[</span><span class="bp">NSLayoutConstraint</span> <span class="nl">constraintsWithVisualFormat</span><span class="p">:</span><span class="s">@&quot;V:[quickbtn(==44)]-10-|&quot;</span> <span class="nl">options</span><span class="p">:</span><span class="mi">0</span> <span class="nl">metrics</span><span class="p">:</span><span class="nb">nil</span> <span class="nl">views</span><span class="p">:</span><span class="n">views</span><span class="p">]];</span>
</span><span class='line'>
</span><span class='line'><span class="c1">//V:表示竖向，[nextbtn(quickbtn)]表示nextbtn与quickbtn等高，-10-|表示nextbtn距离底部10像素</span>
</span><span class='line'><span class="p">[</span><span class="nb">self</span><span class="p">.</span><span class="n">view</span> <span class="nl">addConstraints</span><span class="p">:[</span><span class="bp">NSLayoutConstraint</span> <span class="nl">constraintsWithVisualFormat</span><span class="p">:</span><span class="s">@&quot;V:[nextbtn(quickbtn)]-10-|&quot;</span> <span class="nl">options</span><span class="p">:</span><span class="mi">0</span> <span class="nl">metrics</span><span class="p">:</span><span class="nb">nil</span> <span class="nl">views</span><span class="p">:</span><span class="n">views</span><span class="p">]];</span>
</span><span class='line'>
</span><span class='line'><span class="c1">//H:表示横向，|-10-表示距离左边10像素，中间的-1-表示两个按钮相间1像素，[nextbtn(quickbtn)]表示等宽，-10-|表示距离右边10像素</span>
</span><span class='line'><span class="p">[</span><span class="nb">self</span><span class="p">.</span><span class="n">view</span> <span class="nl">addConstraints</span><span class="p">:[</span><span class="bp">NSLayoutConstraint</span> <span class="nl">constraintsWithVisualFormat</span><span class="p">:</span><span class="s">@&quot;H:|-10-[quickbtn]-1-[nextbtn(quickbtn)]-10-|&quot;</span> <span class="nl">options</span><span class="p">:</span><span class="mi">0</span> <span class="nl">metrics</span><span class="p">:</span><span class="nb">nil</span> <span class="nl">views</span><span class="p">:</span><span class="n">views</span><span class="p">]];</span>
</span></code></pre></td></tr></table></div></figure>


<ol>
<li>看起来很麻烦，习惯了会比算坐标方便很多，因为只要制定距离superview的左右margin和高宽就好了。</li>
</ol>


<h2>效果如下</h2>

<p><img src="/images/constraintsWithVisualFormat.png" alt="VFL演示效果" /></p>

<h2>参考链接</h2>

<ol>
<li><a href="http://blog.sina.com.cn/s/blog_4cd8dd130101t0cd.html">Autolayout 中的Visual format language</a></li>
<li><a href="http://www.cocoachina.com/ios/20141209/10549.html">使用Auto Layout中的VFL(Visual format language)&ndash;代码实现自动布局</a></li>
</ol>

</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/09/10/markdowntian-jia-you-ku-shi-pin/">Markdown添加优酷视频</a></h1>
    
    
      <p class="meta">
        




<time class='entry-date' datetime='2015-09-10T23:52:16+08:00'><span class='date'><span class='date-month'>Sep</span> <span class='date-day'>10</span><span class='date-suffix'>th</span>, <span class='date-year'>2015</span></span> <span class='time'>11:52 pm</span></time>
        
      </p>
    
  </header>


  <div class="entry-content"><h2>代码如下：</h2>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="err">海南骑行</span><span class="nl">Style</span><span class="p">:</span>
</span><span class='line'><span class="o">&lt;</span><span class="n">iframe</span> <span class="n">height</span><span class="o">=</span><span class="mi">450</span> <span class="n">width</span><span class="o">=</span><span class="mi">600</span> <span class="n">src</span><span class="o">=</span><span class="s">&quot;http://player.youku.com/embed/XNDYyODQ5MTI4&quot;</span> <span class="n">frameborder</span><span class="o">=</span><span class="mi">0</span> <span class="n">allowfullscreen</span><span class="o">&gt;&lt;/</span><span class="n">iframe</span><span class="o">&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<ol>
<li>加个标题【海南骑行Style:  】后面加两个空字符表示换行；</li>
<li>获取ID，找到要播放的视频，比如：<a href="http://v.youku.com/v_show/id_XNDYyODQ5MTI4.html">http://v.youku.com/v_show/id_XNDYyODQ5MTI4.html</a> ，可以得到ID为XNDYyODQ5MTI4；</li>
<li>替换到src中结果为 <a href="http://player.youku.com/embed/XNDYyODQ5MTI4">http://player.youku.com/embed/XNDYyODQ5MTI4</a> 就成功了。</li>
<li>视频框的长宽可以自己设置，而且还没有广告:)</li>
</ol>


<h2>效果展示</h2>

<p>海南骑行Style:</p>

<iframe height=450 width=600 src="http://player.youku.com/embed/XNDYyODQ5MTI4" frameborder=0 allowfullscreen></iframe>

</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/09/10/iosqiang-zhi-zhuan-ping/">IOS强制转屏</a></h1>
    
    
      <p class="meta">
        




<time class='entry-date' datetime='2015-09-10T22:05:28+08:00'><span class='date'><span class='date-month'>Sep</span> <span class='date-day'>10</span><span class='date-suffix'>th</span>, <span class='date-year'>2015</span></span> <span class='time'>10:05 pm</span></time>
        
      </p>
    
  </header>


  <div class="entry-content"><h2>问题</h2>

<p>由于应用的特殊性，iPhone5以上的手机必须倒过来使用。就出现了一个屏幕旋转的问题。而且还不能用手机的陀螺仪自动旋转。</p>

<h2>解决办法</h2>

<ol>
<li>关闭自动旋转，在UINavigationController的拓展里实现shouldAutorotate返回NO</li>
</ol>


<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="k">@implementation</span> <span class="bp">UINavigationController</span> <span class="nl">(nonAutoRotation)</span>
</span><span class='line'>
</span><span class='line'><span class="p">-(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nf">shouldAutorotate</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="nb">NO</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">@end</span>
</span></code></pre></td></tr></table></div></figure>


<p>注意是UINavigationController，而不是UIViewController。</p>

<ol>
<li>在UIWindow的拓展里面添加以下方法</li>
</ol>


<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="k">@implementation</span> <span class="bp">UIWindow</span><span class="nl">(Category)</span>
</span><span class='line'>
</span><span class='line'><span class="p">-(</span><span class="kt">void</span><span class="p">)</span><span class="nf">setOrientationUpsideDown:</span><span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nv">upsideDown</span> <span class="nf">animated:</span><span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nv">animated</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="n">UIInterfaceOrientation</span> <span class="n">interfaceOrientation</span> <span class="o">=</span> <span class="n">upsideDown</span> <span class="o">?</span> <span class="nl">UIInterfaceOrientationPortraitUpsideDown</span> <span class="p">:</span> <span class="n">UIInterfaceOrientationPortrait</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">//Get the animation time</span>
</span><span class='line'>    <span class="n">CGFloat</span> <span class="n">duration</span> <span class="o">=</span> <span class="p">[</span><span class="bp">UIApplication</span> <span class="n">sharedApplication</span><span class="p">].</span><span class="n">statusBarOrientationAnimationDuration</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="p">[</span><span class="bp">UIView</span> <span class="nl">beginAnimations</span><span class="p">:</span><span class="nb">nil</span> <span class="nl">context</span><span class="p">:</span><span class="nb">nil</span><span class="p">];</span>
</span><span class='line'>    <span class="p">[</span><span class="bp">UIView</span> <span class="nl">setAnimationDuration</span><span class="p">:</span><span class="n">animated</span> <span class="o">?</span> <span class="nl">duration</span> <span class="p">:</span> <span class="mi">0</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">//Set Status bar orientation</span>
</span><span class='line'>    <span class="p">[[</span><span class="bp">UIApplication</span> <span class="n">sharedApplication</span><span class="p">]</span> <span class="nl">setStatusBarOrientation</span><span class="p">:</span><span class="n">interfaceOrientation</span> <span class="nl">animated</span><span class="p">:</span><span class="nb">YES</span><span class="p">];</span>
</span><span class='line'>    <span class="k">if</span><span class="p">(</span><span class="n">upsideDown</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="c1">//Rotate the windows to portrait upsidedown</span>
</span><span class='line'>        <span class="nb">self</span><span class="p">.</span><span class="n">transform</span><span class="o">=</span><span class="n">CGAffineTransformMakeRotation</span><span class="p">(</span><span class="n">M_PI</span><span class="p">);</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="k">else</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="c1">//Restore it to portrait</span>
</span><span class='line'>        <span class="nb">self</span><span class="p">.</span><span class="n">transform</span> <span class="o">=</span> <span class="n">CGAffineTransformIdentity</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="p">[</span><span class="bp">UIView</span> <span class="n">commitAnimations</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">//Set the device orientation</span>
</span><span class='line'>    <span class="n">UIDeviceOrientation</span> <span class="n">deviceOrientation</span> <span class="o">=</span> <span class="n">upsideDown</span> <span class="o">?</span> <span class="nl">UIDeviceOrientationPortraitUpsideDown</span> <span class="p">:</span> <span class="n">UIDeviceOrientationPortrait</span><span class="p">;</span>
</span><span class='line'>    <span class="p">[[</span><span class="bp">UIDevice</span> <span class="n">currentDevice</span><span class="p">]</span> <span class="nl">setValue</span><span class="p">:</span><span class="l">@(</span><span class="n">deviceOrientation</span><span class="l">)</span> <span class="nl">forKey</span><span class="p">:</span><span class="s">@&quot;orientation&quot;</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">//This method is OK too</span>
</span><span class='line'>    <span class="c1">//[[UIDevice currentDevice] setValuesForKeysWithDictionary:@{@&quot;orientation&quot;: @(deviceOrientation)}];</span>
</span><span class='line'>
</span><span class='line'>    <span class="p">[</span><span class="nb">self</span> <span class="n">setNeedsLayout</span><span class="p">];</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">@end</span>
</span></code></pre></td></tr></table></div></figure>


<p>如果Device的orientation不设置的话，转屏之后在输入的时候键盘还是会从原来的方向弹出来。</p>

<h2>说明</h2>

<p>该例子只支持Portrait的两个方向，其他的方向自然也是一样的，就是旋转的角度不一样而已。</p>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/08/17/iosshe-ji-mo-shi/">IOS设计模式</a></h1>
    
    
      <p class="meta">
        




<time class='entry-date' datetime='2015-08-17T16:20:25+08:00'><span class='date'><span class='date-month'>Aug</span> <span class='date-day'>17</span><span class='date-suffix'>th</span>, <span class='date-year'>2015</span></span> <span class='time'>4:20 pm</span></time>
        
      </p>
    
  </header>


  <div class="entry-content"><p>记录几个关于IOS的设计模式，方便以后查看</p>

<ul>
<li><a href="http://xmuzyq.iteye.com/blog/1942376">IOS设计模式之一（MVC模式，单例模式）</a></li>
<li><a href="http://xmuzyq.iteye.com/blog/1942379">IOS设计模式之二（门面模式，装饰器模式）</a></li>
<li><a href="http://xmuzyq.iteye.com/blog/1942381">IOS设计模式之三（适配器模式，观察者模式）</a></li>
<li><a href="http://xmuzyq.iteye.com/blog/1942386">IOS设计模式之四（备忘录模式，命令模式）</a></li>
</ul>

</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/07/09/at-ioszi-ding-yi-bu-deng-gao-uitableviewcell/">IOS自定义不等高UITableViewCell</a></h1>
    
    
      <p class="meta">
        




<time class='entry-date' datetime='2015-07-09T16:59:08+08:00'><span class='date'><span class='date-month'>Jul</span> <span class='date-day'>9</span><span class='date-suffix'>th</span>, <span class='date-year'>2015</span></span> <span class='time'>4:59 pm</span></time>
        
      </p>
    
  </header>


  <div class="entry-content"><p>在APP中常有类似用户手册问题解答的需求，常常是一问一答的形式。这时候可能问题和答案的长度都不一样，如果用等高的Cell来显示会很难看。这时候就需要不等高的Cell，也就是希望Cell能够自适应高度。这里采用基本的MVC架构。</p>

<h2>Model</h2>

<h3>数据存储</h3>

<p>由于考虑到多平台的通用性，选择json保存数据，这里用的Pod管理的MJExtension库,方便把json转化为Model，先创建一个questions.json的文件。：</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='json'><span class='line'><span class="p">[</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>    <span class="nt">&quot;ID&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
</span><span class='line'>    <span class="nt">&quot;question&quot;</span><span class="p">:</span> <span class="s2">&quot;我的******为什么不可以用？&quot;</span><span class="p">,</span>
</span><span class='line'>    <span class="nt">&quot;answer&quot;</span><span class="p">:</span> <span class="s2">&quot;1. 确保打开手机麦克风权限，IOS手机请前往‘设置-通用-隐私-麦克风’，找到******并打开；\n2. 确保手机遥控器充满电；\n3. 插入是确保能看到‘******’的信息。&quot;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>


<p>内容是一个数组，每一项有3个数据:ID、question和answer。</p>

<h3>ManualModel</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="k">@interface</span> <span class="nc">ManualModel</span> : <span class="bp">NSObject</span>
</span><span class='line'>
</span><span class='line'><span class="k">@property</span> <span class="p">(</span><span class="k">nonatomic</span><span class="p">,</span> <span class="k">assign</span><span class="p">)</span> <span class="bp">NSInteger</span> <span class="n">ID</span><span class="p">;</span>
</span><span class='line'><span class="k">@property</span> <span class="p">(</span><span class="k">nonatomic</span><span class="p">,</span> <span class="k">copy</span><span class="p">)</span> <span class="bp">NSString</span> <span class="o">*</span><span class="n">question</span><span class="p">;</span>
</span><span class='line'><span class="k">@property</span> <span class="p">(</span><span class="k">nonatomic</span><span class="p">,</span> <span class="k">copy</span><span class="p">)</span> <span class="bp">NSString</span> <span class="o">*</span><span class="n">answer</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">@end</span>
</span></code></pre></td></tr></table></div></figure>


<p>ManualModel中的三项和json的一一对应，不对应的话也可以，就是要在转化的时候手动指定就可以了，具体请看<a href="https://github.com/CoderMJLee/MJExtension">MJExtension</a>的讲解。关于生成json对应的Model，有人做了一个Xcode插件<a href="https://github.com/EnjoySR/ESJsonFormat-Xcode">ESJsonFormat-Xcode</a>，图形化快速生成所有的属性，还避免了手动敲代码导致的可能某个属性名差一个字母这些低级错误，用起来还是很方便的。</p>

<h2>View</h2>

<h3>.h文件</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="k">@class</span> <span class="nc">ManualModel</span>;
</span><span class='line'><span class="k">@interface</span> <span class="nc">UnequalHeightCell</span> : <span class="bp">UITableViewCell</span>
</span><span class='line'>
</span><span class='line'><span class="k">@property</span> <span class="p">(</span><span class="k">nonatomic</span><span class="p">,</span> <span class="k">strong</span><span class="p">)</span><span class="n">ManualModel</span> <span class="o">*</span><span class="n">manual</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="p">+(</span><span class="n">CGFloat</span><span class="p">)</span><span class="nf">cellHeight:</span><span class="p">(</span><span class="n">ManualModel</span><span class="o">*</span><span class="p">)</span><span class="nv">model</span><span class="p">;</span>
</span><span class='line'><span class="k">@end</span>
</span></code></pre></td></tr></table></div></figure>


<p>创建UnequalHeightCell，添加属性manual，以及类的方法cellHeight用来计算cell 的高度。</p>

<h3>.m文件</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="k">@implementation</span> <span class="nc">UnequalHeightCell</span><span class="p">{</span>
</span><span class='line'>    <span class="bp">UIView</span> <span class="o">*</span><span class="n">questionView</span><span class="p">,</span> <span class="o">*</span><span class="n">answerView</span><span class="p">;</span>
</span><span class='line'>    <span class="bp">UILabel</span> <span class="o">*</span><span class="n">questionLabel</span><span class="p">,</span><span class="o">*</span><span class="n">answerLabel</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="p">-(</span><span class="kt">instancetype</span><span class="p">)</span><span class="nf">initWithStyle:</span><span class="p">(</span><span class="n">UITableViewCellStyle</span><span class="p">)</span><span class="nv">style</span> <span class="nf">reuseIdentifier:</span><span class="p">(</span><span class="bp">NSString</span> <span class="o">*</span><span class="p">)</span><span class="nv">reuseIdentifier</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="nb">self</span> <span class="o">=</span> <span class="p">[</span><span class="nb">super</span> <span class="nl">initWithStyle</span><span class="p">:</span><span class="n">style</span> <span class="nl">reuseIdentifier</span><span class="p">:</span><span class="n">reuseIdentifier</span><span class="p">];</span>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="nb">self</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="n">questionView</span><span class="o">=</span><span class="p">[[</span><span class="bp">UIView</span> <span class="n">alloc</span><span class="p">]</span><span class="nl">initWithFrame</span><span class="p">:</span><span class="n">CGRectZero</span><span class="p">];</span>
</span><span class='line'>        <span class="n">questionView</span><span class="p">.</span><span class="n">backgroundColor</span><span class="o">=</span><span class="p">[</span><span class="bp">UIColor</span> <span class="nl">colorWithRed</span><span class="p">:</span><span class="mi">0</span> <span class="nl">green</span><span class="p">:</span><span class="mf">0.75</span> <span class="nl">blue</span><span class="p">:</span><span class="mi">1</span> <span class="nl">alpha</span><span class="p">:</span><span class="mi">1</span><span class="p">];</span>
</span><span class='line'>        <span class="p">[</span><span class="nb">self</span><span class="p">.</span><span class="n">contentView</span> <span class="nl">addSubview</span><span class="p">:</span><span class="n">questionView</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">questionLabel</span><span class="o">=</span><span class="p">[</span><span class="nb">self</span> <span class="nl">labelWithFont</span><span class="p">:</span><span class="n">QFont</span> <span class="nl">textColor</span><span class="p">:[</span><span class="bp">UIColor</span> <span class="n">whiteColor</span><span class="p">]];</span>
</span><span class='line'>        <span class="p">[</span><span class="n">questionView</span> <span class="nl">addSubview</span><span class="p">:</span><span class="n">questionLabel</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">answerView</span><span class="o">=</span><span class="p">[[</span><span class="bp">UIView</span> <span class="n">alloc</span><span class="p">]</span><span class="nl">initWithFrame</span><span class="p">:</span><span class="n">CGRectZero</span><span class="p">];</span>
</span><span class='line'>        <span class="n">answerView</span><span class="p">.</span><span class="n">backgroundColor</span><span class="o">=</span><span class="p">[</span><span class="bp">UIColor</span> <span class="n">whiteColor</span><span class="p">];</span>
</span><span class='line'>        <span class="p">[</span><span class="nb">self</span><span class="p">.</span><span class="n">contentView</span> <span class="nl">addSubview</span><span class="p">:</span><span class="n">answerView</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">answerLabel</span><span class="o">=</span><span class="p">[</span><span class="nb">self</span> <span class="nl">labelWithFont</span><span class="p">:</span><span class="n">AFont</span> <span class="nl">textColor</span><span class="p">:[</span><span class="bp">UIColor</span> <span class="n">blackColor</span><span class="p">]];</span>
</span><span class='line'>        <span class="p">[</span><span class="n">answerView</span> <span class="nl">addSubview</span><span class="p">:</span><span class="n">answerLabel</span><span class="p">];</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="k">return</span> <span class="nb">self</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="p">-(</span><span class="bp">UILabel</span><span class="o">*</span><span class="p">)</span><span class="nf">labelWithFont:</span><span class="p">(</span><span class="bp">UIFont</span><span class="o">*</span><span class="p">)</span><span class="nv">font</span> <span class="nf">textColor:</span><span class="p">(</span><span class="bp">UIColor</span><span class="o">*</span><span class="p">)</span><span class="nv">textColor</span><span class="p">{</span>
</span><span class='line'>    <span class="bp">UILabel</span> <span class="o">*</span><span class="n">label</span><span class="o">=</span><span class="p">[[</span><span class="bp">UILabel</span> <span class="n">alloc</span><span class="p">]</span><span class="nl">initWithFrame</span><span class="p">:</span><span class="n">CGRectZero</span><span class="p">];</span>
</span><span class='line'>    <span class="n">label</span><span class="p">.</span><span class="n">backgroundColor</span><span class="o">=</span><span class="p">[</span><span class="bp">UIColor</span> <span class="n">clearColor</span><span class="p">];</span>
</span><span class='line'>    <span class="n">label</span><span class="p">.</span><span class="n">textColor</span><span class="o">=</span><span class="n">textColor</span><span class="p">;</span>
</span><span class='line'>    <span class="n">label</span><span class="p">.</span><span class="n">font</span><span class="o">=</span><span class="n">font</span><span class="p">;</span>
</span><span class='line'>    <span class="n">label</span><span class="p">.</span><span class="n">numberOfLines</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
</span><span class='line'>    <span class="n">label</span><span class="p">.</span><span class="n">textAlignment</span><span class="o">=</span><span class="n">NSTextAlignmentLeft</span><span class="p">;</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">label</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>实现初始化函数initWithStyle，放两个label分别显示问题和答案，考虑的上下左右的边缘，加个UIView放置label。记得把questionView和answerView添加到contentView上，这样子做的好处是如果需要进入编辑模式，他会自动缩进。frame可以放到layoutSubviews中统一处理。关于上下左右留出边缘的方法还有一种，就是自定义一个UILabel重写以下方法：</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="k">@property</span><span class="p">(</span><span class="k">nonatomic</span><span class="p">)</span> <span class="bp">UIEdgeInsets</span> <span class="n">insets</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="p">-(</span><span class="kt">void</span><span class="p">)</span><span class="nf">drawTextInRect:</span><span class="p">(</span><span class="bp">CGRect</span><span class="p">)</span><span class="nv">rect</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="p">[</span><span class="nb">super</span> <span class="nl">drawTextInRect</span><span class="p">:</span><span class="n">UIEdgeInsetsInsetRect</span><span class="p">(</span><span class="n">rect</span><span class="p">,</span> <span class="nb">self</span><span class="p">.</span><span class="n">insets</span><span class="p">)];</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>这样子要注意的是：根据内容计算出高度之后，就要自己加上上下所留的才是整个Label的高度。在本例中还是用笨一点的方法算了。</p>

<h3>实现manual的set函数</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="p">-(</span><span class="kt">void</span><span class="p">)</span><span class="nf">setManual:</span><span class="p">(</span><span class="n">ManualModel</span> <span class="o">*</span><span class="p">)</span><span class="nv">manual</span><span class="p">{</span>
</span><span class='line'>    <span class="n">_manual</span><span class="o">=</span><span class="n">manual</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">questionLabel</span><span class="p">.</span><span class="n">text</span><span class="o">=</span><span class="p">[</span><span class="bp">NSString</span> <span class="nl">stringWithFormat</span><span class="p">:</span><span class="s">@&quot;Q%d: %@&quot;</span><span class="p">,(</span><span class="kt">int</span><span class="p">)</span><span class="n">manual</span><span class="p">.</span><span class="n">ID</span><span class="p">,</span><span class="n">manual</span><span class="p">.</span><span class="n">question</span><span class="p">];</span>
</span><span class='line'>    <span class="n">answerLabel</span><span class="p">.</span><span class="n">text</span><span class="o">=</span><span class="n">manual</span><span class="p">.</span><span class="n">answer</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="p">[</span><span class="nb">self</span> <span class="n">setNeedsLayout</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="p">-(</span><span class="kt">void</span><span class="p">)</span><span class="nf">layoutSubviews</span><span class="p">{</span>
</span><span class='line'>    <span class="p">[</span><span class="nb">super</span> <span class="n">layoutSubviews</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">//设置问题的frame</span>
</span><span class='line'>    <span class="n">CGFloat</span> <span class="n">height</span><span class="o">=</span><span class="p">[</span><span class="n">UnequalHeightCell</span> <span class="nl">height</span><span class="p">:</span><span class="n">_manual</span><span class="p">.</span><span class="n">question</span> <span class="nl">font</span><span class="p">:</span><span class="n">QFont</span><span class="p">];</span>
</span><span class='line'>    <span class="n">questionView</span><span class="p">.</span><span class="n">frame</span><span class="o">=</span><span class="n">CGRectMake</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">ScreenWidth</span><span class="p">,</span> <span class="n">height</span><span class="p">);</span>
</span><span class='line'>    <span class="n">questionLabel</span><span class="p">.</span><span class="n">frame</span><span class="o">=</span><span class="n">CGRectInset</span><span class="p">(</span><span class="n">questionView</span><span class="p">.</span><span class="n">bounds</span><span class="p">,</span> <span class="n">LeftRightMargin</span><span class="p">,</span> <span class="n">TopBottomMargin</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">//设置答案的frame</span>
</span><span class='line'>    <span class="n">height</span><span class="o">=</span><span class="p">[</span><span class="n">UnequalHeightCell</span> <span class="nl">height</span><span class="p">:</span><span class="n">_manual</span><span class="p">.</span><span class="n">answer</span> <span class="nl">font</span><span class="p">:</span><span class="n">AFont</span><span class="p">];</span>
</span><span class='line'>    <span class="n">answerView</span><span class="p">.</span><span class="n">frame</span><span class="o">=</span><span class="n">CGRectMake</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">questionView</span><span class="p">.</span><span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">height</span><span class="p">,</span> <span class="n">ScreenWidth</span><span class="p">,</span> <span class="n">height</span><span class="p">);</span>
</span><span class='line'>    <span class="n">answerLabel</span><span class="p">.</span><span class="n">frame</span><span class="o">=</span><span class="n">CGRectInset</span><span class="p">(</span><span class="n">answerView</span><span class="p">.</span><span class="n">bounds</span><span class="p">,</span> <span class="n">LeftRightMargin</span><span class="p">,</span> <span class="n">TopBottomMargin</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>在调用<code>[self setNeedsLayout];</code>的时候会执行layoutSubviews，设置所有控件的frame。</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="c1">//根据字体计算问题和答案View需要的高度</span>
</span><span class='line'><span class="p">+(</span><span class="n">CGFloat</span><span class="p">)</span><span class="nf">height:</span><span class="p">(</span><span class="bp">NSString</span><span class="o">*</span><span class="p">)</span><span class="nv">text</span> <span class="nf">font:</span><span class="p">(</span><span class="bp">UIFont</span><span class="o">*</span><span class="p">)</span><span class="nv">font</span><span class="p">{</span>
</span><span class='line'>    <span class="bp">CGSize</span> <span class="n">size</span><span class="o">=</span><span class="p">[</span><span class="n">text</span> <span class="nl">sizeWithFont</span><span class="p">:</span><span class="n">font</span> <span class="nl">constrainedToSize</span><span class="p">:</span><span class="n">CGSizeMake</span><span class="p">(</span><span class="n">ScreenWidth</span><span class="o">-</span><span class="mi">2</span><span class="o">*</span><span class="n">LeftRightMargin</span><span class="p">,</span> <span class="mi">2000</span><span class="p">)</span> <span class="nl">lineBreakMode</span><span class="p">:</span><span class="n">NSLineBreakByWordWrapping</span><span class="p">];</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">MAX</span><span class="p">(</span><span class="n">MinHeight</span><span class="p">,</span> <span class="n">ceilf</span><span class="p">(</span><span class="n">size</span><span class="p">.</span><span class="n">height</span><span class="p">)</span><span class="o">+</span><span class="mi">2</span><span class="o">*</span><span class="n">TopBottomMargin</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">//Cell的总高度</span>
</span><span class='line'><span class="p">+(</span><span class="n">CGFloat</span><span class="p">)</span><span class="nf">cellHeight:</span><span class="p">(</span><span class="n">ManualModel</span><span class="o">*</span><span class="p">)</span><span class="nv">model</span><span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="p">[</span><span class="nb">self</span> <span class="nl">height</span><span class="p">:</span><span class="n">model</span><span class="p">.</span><span class="n">question</span> <span class="nl">font</span><span class="p">:</span><span class="n">QFont</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="nb">self</span> <span class="nl">height</span><span class="p">:</span><span class="n">model</span><span class="p">.</span><span class="n">answer</span> <span class="nl">font</span><span class="p">:</span><span class="n">AFont</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>其中sizeWithFont是<a href="http://loongwoo.github.io/blog/2015/07/08/at-ios-nsstringchang-yong-category/">IOS NSString常用Category</a>用了的。cellHeight是构造不等高的cell所必须的。他是questionView和answerView的两个高度和。如果控件更多，计算就复杂一点，意思是一样的。</p>

<h2>Controller</h2>

<p>ViewController继承于UITableViewController，先获取数据源</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="k">@property</span> <span class="p">(</span><span class="k">nonatomic</span><span class="p">,</span> <span class="k">strong</span><span class="p">)</span><span class="bp">NSArray</span> <span class="o">*</span><span class="n">questionAndAnswerArr</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="p">-(</span><span class="bp">NSArray</span> <span class="o">*</span><span class="p">)</span><span class="nf">questionAndAnswerArr</span><span class="p">{</span>
</span><span class='line'>    <span class="k">if</span><span class="p">(</span><span class="n">_questionAndAnswerArr</span><span class="o">==</span><span class="nb">nil</span><span class="p">){</span>
</span><span class='line'>        <span class="bp">NSString</span> <span class="o">*</span><span class="n">path</span><span class="o">=</span><span class="p">[[</span><span class="bp">NSBundle</span> <span class="n">mainBundle</span><span class="p">]</span> <span class="nl">pathForResource</span><span class="p">:</span><span class="s">@&quot;questions&quot;</span> <span class="nl">ofType</span><span class="p">:</span><span class="s">@&quot;json&quot;</span><span class="p">];</span>
</span><span class='line'>        <span class="bp">NSData</span> <span class="o">*</span><span class="n">data</span><span class="o">=</span><span class="p">[</span><span class="bp">NSData</span> <span class="nl">dataWithContentsOfFile</span><span class="p">:</span><span class="n">path</span><span class="p">];</span>
</span><span class='line'>        <span class="bp">NSArray</span> <span class="o">*</span><span class="n">tmp</span><span class="o">=</span><span class="p">[</span><span class="n">data</span> <span class="n">JSONObject</span><span class="p">];</span>
</span><span class='line'>        <span class="n">_questionAndAnswerArr</span><span class="o">=</span><span class="p">[</span><span class="n">ManualModel</span> <span class="nl">objectArrayWithKeyValuesArray</span><span class="p">:</span><span class="n">tmp</span><span class="p">];</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">_questionAndAnswerArr</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>objectArrayWithKeyValuesArray是MJExtension中的方法，可以把字典的数组转化为Model的数组。然后在实现UITableViewDataSource的三个方法</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="cp">#pragma UITableViewDelegate &amp; UITableViewDataSource</span>
</span><span class='line'><span class="p">-(</span><span class="bp">NSInteger</span><span class="p">)</span><span class="nf">tableView:</span><span class="p">(</span><span class="bp">UITableView</span> <span class="o">*</span><span class="p">)</span><span class="nv">tableView</span> <span class="nf">numberOfRowsInSection:</span><span class="p">(</span><span class="bp">NSInteger</span><span class="p">)</span><span class="nv">section</span><span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="nb">self</span><span class="p">.</span><span class="n">questionAndAnswerArr</span><span class="p">.</span><span class="n">count</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="p">-(</span><span class="n">CGFloat</span><span class="p">)</span><span class="nf">tableView:</span><span class="p">(</span><span class="bp">UITableView</span> <span class="o">*</span><span class="p">)</span><span class="nv">tableView</span> <span class="nf">heightForRowAtIndexPath:</span><span class="p">(</span><span class="bp">NSIndexPath</span> <span class="o">*</span><span class="p">)</span><span class="nv">indexPath</span><span class="p">{</span>
</span><span class='line'>    <span class="n">ManualModel</span> <span class="o">*</span><span class="n">model</span><span class="o">=</span><span class="nb">self</span><span class="p">.</span><span class="n">questionAndAnswerArr</span><span class="p">[</span><span class="n">indexPath</span><span class="p">.</span><span class="n">row</span><span class="p">];</span>
</span><span class='line'>    <span class="k">return</span> <span class="p">[</span><span class="n">UnequalHeightCell</span> <span class="nl">cellHeight</span><span class="p">:</span><span class="n">model</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="p">-(</span><span class="bp">UITableViewCell</span><span class="o">*</span><span class="p">)</span><span class="nf">tableView:</span><span class="p">(</span><span class="bp">UITableView</span> <span class="o">*</span><span class="p">)</span><span class="nv">tableView</span> <span class="nf">cellForRowAtIndexPath:</span><span class="p">(</span><span class="bp">NSIndexPath</span> <span class="o">*</span><span class="p">)</span><span class="nv">indexPath</span><span class="p">{</span>
</span><span class='line'>    <span class="k">static</span> <span class="bp">NSString</span> <span class="o">*</span><span class="n">identity</span><span class="o">=</span><span class="s">@&quot;mycell&quot;</span><span class="p">;</span>
</span><span class='line'>    <span class="n">UnequalHeightCell</span> <span class="o">*</span><span class="n">cell</span><span class="o">=</span><span class="p">[</span><span class="n">tableView</span> <span class="nl">dequeueReusableCellWithIdentifier</span><span class="p">:</span><span class="n">identity</span><span class="p">];</span>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="n">cell</span><span class="o">==</span><span class="nb">nil</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="n">cell</span><span class="o">=</span><span class="p">[[</span><span class="n">UnequalHeightCell</span> <span class="n">alloc</span><span class="p">]</span><span class="nl">initWithStyle</span><span class="p">:</span><span class="n">UITableViewCellStyleDefault</span> <span class="nl">reuseIdentifier</span><span class="p">:</span><span class="n">identity</span><span class="p">];</span>
</span><span class='line'>        <span class="n">cell</span><span class="p">.</span><span class="n">selectionStyle</span><span class="o">=</span><span class="n">UITableViewCellSelectionStyleNone</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="n">cell</span><span class="p">.</span><span class="n">manual</span><span class="o">=</span><span class="nb">self</span><span class="p">.</span><span class="n">questionAndAnswerArr</span><span class="p">[</span><span class="n">indexPath</span><span class="p">.</span><span class="n">row</span><span class="p">];</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">cell</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="cp">#pragma UITableViewDelegate &amp; UITableViewDataSource</span>
</span></code></pre></td></tr></table></div></figure>


<p>注意到heightForRowAtIndexPath中返回的不是一个固定的值了。而是根据数据源算出来的值。这就实现了不等高的Cell。</p>

<h2>效果展示</h2>

<p><img src="/images/unequalcell.png" alt="效果图" /></p>

<h2>源码下载</h2>

<p><a href="https://github.com/Loongwoo/UnequalHeightDemo">UnequalHeightCellDemo</a></p>
</div>
  
  


    </article>
  
  
    <article>
      
  <header>
    
      <h1 class="entry-title"><a href="/blog/2015/07/09/at-iosxiao-xi-zhuan-fa-ji-zhi/">IOS消息转发机制</a></h1>
    
    
      <p class="meta">
        




<time class='entry-date' datetime='2015-07-09T11:14:13+08:00'><span class='date'><span class='date-month'>Jul</span> <span class='date-day'>9</span><span class='date-suffix'>th</span>, <span class='date-year'>2015</span></span> <span class='time'>11:14 am</span></time>
        
      </p>
    
  </header>


  <div class="entry-content"><h3>介绍</h3>

<p>平时基本很少用的的知识点，但是知其所以然还是有好处的。以下的Demo完全参考最下面的链接的文章写的，理解起来不难。</p>

<h3>源码下载</h3>

<p><a href="https://github.com/Loongwoo/MsgForwardDemo">MsgForwardDemo</a></p>

<h3>参考链接</h3>

<p><a href="http://www.jianshu.com/p/1bde36ad9938">http://www.jianshu.com/p/1bde36ad9938</a></p>
</div>
  
  


    </article>
  
  <div class="pagination">
    
      <a class="prev" href="/posts/2">&larr; Older</a>
    
    <a href="/blog/archives">Blog Archives</a>
    
  </div>
</div>
<aside class="sidebar">
  
    <section>
  <h1>最新文章</h1>
  <ul id="recent_posts">
    
      <li class="post">
        <a href="/blog/2016/01/28/stringmanage-a-xcode-plugin-for-managing-your-strings/">StringManage -- a Xcode Plugin for Managing Your Strings</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/09/12/iosshi-yong-bundle/">iOS使用Bundle</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/09/12/iosduo-ge-localizable-dot-strings/">IOS多个Localizable.strings</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/09/11/iosxian-zhi-uitextfieldchang-du/">IOS限制UITextField长度</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/09/11/ioszi-dong-bu-ju-constraintswithvisualformat/">IOS自动布局constraintsWithVisualFormat</a>
      </li>
    
  </ul>
</section>




<section>
	<h1>关于我</h1>
    <p>程序员、创业者、IOS开发工程师
	<br/>新浪微博：<a href='http://weibo.com/loongwoo' target='_blank'>商陆_Xu</a>
	<br/>新浪博客：<a href="http://blog.sina.com.cn/lunaize" target="_blank" title="商陆的博客">商陆</a>
	<br/>GitHub：<a href='https://github.com/loongwoo' target='_blank'>Loongwoo</a>
	<br/>Git@OSC: <a href='http://git.oschina.net/redder_xu' target='_blank'>redder_xu</a>
    <br/>CSDN博客：<a href="http://blog.csdn.net/redder_xu" target="_blank" title="CSDN">霜叶红于二月花</a>
	<br/>个人微信：xulongwu77 欢迎调戏
	<br/><img width="220px" src="/images/myweixin.jpg" />
	</p>
</section>
<section>
  <h1>友情链接</h1>
  <ul>
  <li><a href="http://blog.devtang.com" target="_blank" title="唐巧的技术博客">唐巧的技术博客</a></li>
  <li><a href="http://www.leichunfeng.com" target="_blank" title="雷纯锋的技术博客">雷纯锋的技术博客</a></li>
  <li><a href="http://www.superqq.com" target="_blank" title=“刚刚在线”>刚刚在线</a></li>
  </ul>
</section>

  
</aside>

    </div>
  </div>
  <footer role="contentinfo"><p>
  Copyright &copy; 2017 - 许龙武 -
  <span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span>
  <script language="javascript" type="text/javascript" src="http://js.users.51.la/17858326.js"></script>
<noscript><a href="http://www.51.la/?17858326" target="_blank"><img alt="&#x6211;&#x8981;&#x5566;&#x514D;&#x8D39;&#x7EDF;&#x8BA1;" src="http://img.users.51.la/17858326.asp" style="border:none" /></a></noscript>

</p>

</footer>
  











</body>
</html>
